package cn.hjc.dao;

import cn.hjc.entity.Dept;
import org.hibernate.Query;
import org.hibernate.Transaction;

import java.io.Serializable;
import java.util.List;

public class DeptDao extends BaseDao {
    public Dept get(Serializable id){
        //通过Session的get()方法根据OID加载指定对象
        return (Dept) HibernateUtil.currentSession().get(Dept.class,id);

    }
    public Dept load(Serializable id){
        //通过Session的load()方法根据OID加载指定对象
        return (Dept) HibernateUtil.currentSession().load(Dept.class,id);
    }

    public void save(Dept dept){
        this.currentSession().save(dept);//保存指定的Dept

    }

    public void delete(Dept dept){
        this.currentSession().delete(this.load(dept.getDeptNo()));  //删除指定的Dept对象
    }

    public Dept merge(Dept dept){
        return  (Dept) currentSession().merge(dept);
    }

    public List<Dept> findByDeptName(String deptName){
        //设置命名参数
        String hql = "from Dept as dept where dept.deptName = :name";
        Query query = currentSession().createQuery(hql);
        query.setString("name","SALES");//为占位符赋值，占位符下标从0开始
        return  query.list();
    }
    public List<String> findAllNames(){
        String hql = "select deptName from Dept";
        return currentSession().createQuery(hql).list();
    }

    public List<Object[]> findAllDeptList(){
        String hql = "select deptNo,deptName from Dept";
        return currentSession().createQuery(hql).list();
    }

    public List<Dept> findAllDeptLists(){
        String hql = "select deptNo,deptName from Dept";
        return currentSession().createQuery(hql).list();
    }
}
